﻿/*
 * File:   sw_timer.h
 * Author: aple
 */

#ifndef TSW_TIMER_H
#define	TSW_TIMER_H

#include "stdint.h"

#ifdef	__cplusplus
extern "C" {
#endif

	/*void task list*/
	struct swtm_cb_str
	{
		uint32_t period;
		uint32_t tick_cnt;
		uint8_t mode;
		uint8_t run_st;
		uint8_t pulse;
		uint8_t wave;
		void(*task)(void);
		struct swtm_cb_str *next;
	};

	typedef struct swtm_cb_str swtm_cb_type;

	struct swtm_manage_str
	{
		uint16_t tick_old;
		swtm_cb_type *list;
	};

	typedef struct swtm_manage_str swtm_manage_type;

	void swtm_init(swtm_manage_type *vect, uint16_t tick);
	void swtm_add(swtm_manage_type *vect, swtm_cb_type * tm_cb, void(*task)(void)); // 如果不需要任务回调，task可设为0
	void swtm_del(swtm_manage_type *vect, swtm_cb_type * tm_cb);
	void swtm_reset(swtm_cb_type * tm_cb); // tick_cnt = 0
	void swtm_start(swtm_cb_type * tm_cb); // run_st = 1
	void swtm_stop(swtm_cb_type * tm_cb);  // run_st = 0
	void swtm_set(swtm_cb_type * tm_cb, uint8_t mode, uint32_t cmp); // mode =mode, period = cmp //mode0-周期 mode1-单次
	void swtm_run(swtm_manage_type *vect, uint16_t tick); // execute


	//device run time
	typedef struct
	{
		int32_t sec_set;    //Y设定时间，单位秒, 设为0时计时但不产生通知和复位
		int32_t sec_cnt;    //X内部管理 
		int8_t rtc_sec_old;  //X内部管理
		int8_t reset_at_end; //Y计时到复位计数器 0-复位，1-继续计数
		int8_t b_time_end;     //输出计时结束标记，外部使用后，由外部清除
	} time_run_handle_type;

	void time_run_handle(time_run_handle_type * obj, int8_t rtc_sec_now);
	void time_run_reset(time_run_handle_type * obj, int8_t rtc_sec_now);


#ifdef	__cplusplus
}
#endif

#endif	/* TSW_TIMER_H */

